// pages/home/home.js
const app = getApp();
import request from "../../utils/request";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    cartCount: 5,
    token: "",
    code: "",
    point:'',
    userPhoneNumber: "",
    mobile: "",
    nickname: "",
    balance: "",
    talExpense: "",
    talRecharge: "",
    userdata: {
      avatar: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/userProfile.png",
    },
    categoryOrderList: [
      { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/payment.png", title: "待付款",method:'gounpaid' },    
      { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/deploy.png", title: "部署中",method:'gorefund' },
      { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/use.png", title: "使用中",method:'gouseing' },
      { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/ordergoods.png", title: "订单",method:'gogoodsorder'},
    ],
    orderList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getbalance();
    this.whetherDiLi()
  },

  gowallet() {
    const talExpense = this.data.talExpense;
    const talRecharge = this.data.talRecharge;
    const balance = this.data.balance;
    wx.navigateTo({
      url: `/pages/wallet/wallet?talExpense=${talExpense}&talRecharge=${talRecharge}&balance=${balance}`,
    });
  },
  async getbalance() {
    const res = await request({
      url: app.globalData.baseURL + "/app-user-api/pay/wallet/get",
      method: "GET",
    });
    let balanceYuan = res.data.balance;
    let balanceInYuan = (balanceYuan / 100).toFixed(2);
    let talExpense = res.data.totalExpense;
    let totalExpense = (talExpense / 100).toFixed(2);
    let talRecharge = res.data.totalRecharge;
    let totalRecharge = (talRecharge / 100).toFixed(2);
    this.setData({
      balance: balanceInYuan,
      talExpense: totalExpense,
      talRecharge: totalRecharge,
    });
  },
  async getuser() {
    try {
      const res = await request({
        url: app.globalData.baseURL + "/app-user-api/member/user/get",
        method: "GET",
      });
      this.setData({
        userdata: res.data,
        mobile: res.data.mobile,
        nickname: res.data.nickname,
        point:res.data.point
      });
      wx.setStorageSync('username', res.data.nickname)
    } catch (error) {
      console.error("请求发生错误：", error);
    }
  },
  login() {
    const encodedUserdata = encodeURIComponent(
      JSON.stringify(this.data.userdata)
    );
    wx.navigateTo({
      url: `../login/login?userdata=${encodedUserdata}`,
    });
  },
  goworkbench() {
    wx.navigateTo({
      url: "/pages/workbench/workbench",
    });
  },
  goposter() {
    wx.navigateTo({
      url: "/pages/poster/poster",
    });
  },
  godaili() {
    wx.navigateTo({
      url: "/pages/daili/daili",
    });
  },
  updateCartBadge() {
    const app = getApp();
    const newCount = app.globalData.cartCount;
    wx.setStorageSync("cartCount", newCount); // 存储到本地存储，以便跨页面访问
    this.setData({ cartCount: newCount }); // 假设你已经在页面的data中定义了cartCount
  },
  getPhone(e) {
    this.setData({
      code: e.detail.code,
    });
    this.getPhoneNumber();
  },
  getPhoneNumber() {
    wx.request({
      url: `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${this.data.token}`,
      method: "POST",
      data: {
        code: this.data.code,
      },
      success(res) {
        this.setData({
          userPhoneNumber: res.data.phone_info.phoneNumber,
        });
      },
    });
  },
  gounpaid(){
    wx.navigateTo({
      url: '/pages/unpaid/unpaid',
    })
  },
  godeploy(){
    wx.navigateTo({
      url: '/pages/deploy/deploy',
    })
  },
  gouseing(){
    wx.navigateTo({
      url: '/pages/useing/useing',
    })
  },
  gorefund(){
    wx.navigateTo({
      url: '/pages/refund/refund',
    })
  },
  gogoodsorder(){
    wx.navigateTo({
      url: '/pages/goodsorder/goodsorder',
    })
  },
  async whetherDiLi(){
    const id = wx.getStorageSync('userId')
    const res = await request({
      url: app.globalData.baseURL + '/app-user-api/member/proxy-user/getProxyUser',
      method:'POST',
      data:{
        "memberId": id,
        "status": 1
      }
    })
    if(res.code === 0){
      wx.setStorageSync('proxyId', res.data[0].id)
      const list = [
        {
          src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/workbench.png",
          title: "工作台",
          method: "goworkbench",
        },
        {
          src: "/images/tabBar/recode.png",
          title: "我的二维码",
          method: "goposter",
        },
        { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/integral.png", title: "积分",method: "gopoint" },
        { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/wallet.png", title: "钱包", method: "gowallet" }
    ]
      this.setData({
        orderList: list
      })
    }else if(res.code === 201){
      const list = [
        {
          src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/agency.png",
          title: "成为代理",
          method: "godaili",
        },
        { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/integral.png", title: "积分" },
        { src: "https://jiazhiqin.oss-cn-chengdu.aliyuncs.com/mini-program/commen/commen/commen/wallet.png", title: "钱包", method: "gowallet" }
    ]
      this.setData({
        orderList: list
      })
    }
  },
   getOrderList() {
    const count = wx.getStorageSync("count");
    this.setData({
      cartCount: count,
    });
  },
  gopointrecord(){
    wx.navigateTo({
      url: '/pages/pointRecord/pointRecord',
    })
  },
  gopoint(){
    wx.navigateTo({
      url: '/pages/pointRecord/pointRecord',
    })
  },
  // 会员积分记录
  // async getPoint(){
  //   const res = await request({
  //     url: app.globalData.baseURL + `/app-user-api/member/point/record/page?pageNo=${1}&pageSize=${100}`,
  //     method:'GET'
  //   })
  //   console.log('1',res);
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.updateCartBadge();
    this.getuser();
    this.whetherDiLi()
    this.getOrderList()
    this.getbalance()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (options) {
    return {
      title: "嘉之勤官网小程序",
      path: "/pages/index/index",
      imageUrl:
        "https://youyougo2.oss-cn-chengdu.aliyuncs.com/0201506f6fc0f4145e34159c2e2ac66b94b96450dd29e5a19d177edc5dad8023.png",
    };
  },
});
